package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "modernc.org/sqlite"
)

func main() {
	db, err := sql.Open("sqlite", "data/workflow.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query(`
		SELECT p.change_id, pr.summary, p.complexity, p.created_at, pr.motivation
		FROM projects p
		LEFT JOIN proposals pr ON pr.project_id = p.id
		ORDER BY p.created_at DESC
		LIMIT 10
	`)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	fmt.Println("Recent workflows:")
	fmt.Println("================")
	count := 0
	for rows.Next() {
		var changeID, summary, complexity, createdAt, motivation string
		if err := rows.Scan(&changeID, &summary, &complexity, &createdAt, &motivation); err != nil {
			log.Fatal(err)
		}
		count++
		fmt.Printf("\n%d. ID: %s\n", count, changeID)
		fmt.Printf("   Summary: %s\n", summary)
		fmt.Printf("   Complexity: %s\n", complexity)
		fmt.Printf("   Created: %s\n", createdAt)
		if len(motivation) > 80 {
			fmt.Printf("   Motivation: %s...\n", motivation[:77])
		} else {
			fmt.Printf("   Motivation: %s\n", motivation)
		}
		fmt.Println("   ---")
	}

	if count == 0 {
		fmt.Println("No workflows found in database.")
	} else {
		fmt.Printf("\nTotal: %d workflows\n", count)
	}
}
